import { _decorator, Component, Node, sys, resources, JsonAsset } from 'cc';
import EventManagerInstance, { GameMsgType } from './EventManagerInstance';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = LangDataInstance
 * DateTime = Tue Apr 19 2022 16:06:25 GMT+0800 (中国标准时间)
 * Author = withtea
 * FileBasename = LangDataInstance.ts
 * FileBasenameNoExtension = LangDataInstance
 * URL = db://assets/scripts/BaseInstance/LangDataInstance.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('LangDataInstance')
export default class LangDataInstance {
    private static instance: LangDataInstance = null;
    private m_language_data = null;
    private m_language_code: string = '';
    public static GetInstance(): LangDataInstance {
        if (LangDataInstance.instance == null) {
            LangDataInstance.instance = new LangDataInstance();
            LangDataInstance.instance.m_language_data = null;
        }
        return this.instance;
    }
    private getLanguageType() {
        console.log('languagecode ' + sys.languageCode);
        if (sys.platform == sys.Platform.ANDROID) {
            this.m_language_code = sys.languageCode;
        } else {
            this.m_language_code = sys.languageCode;
        }
    }
    public SetLanguageData(json_data) {
        this.m_language_data = json_data.LanguageConfig;
    }
    public LoadLangData() {
        LangDataInstance.instance.getLanguageType();
        resources.load('config/cfg', JsonAsset, function (error, res) {
            if (error != null) {
                console.log('language load have problem');
                EventManagerInstance.GetInstance().Notify(GameMsgType.LoadLanguageDataEnd, false);
                return;
            }
            console.log('m_language_data load success');
            LangDataInstance.GetInstance().SetLanguageData(res.json);
            EventManagerInstance.GetInstance().Notify(GameMsgType.LoadLanguageDataEnd, true);
        });
    }
    public GetLangStringByTitle(title_str: string): string {
        if (this.m_language_data == null) {
            return title_str;
        }

        let config = null;
        for (let index = 0; index < this.m_language_data.length; index++) {
            if (this.m_language_data[index].title == title_str) {
                config = this.m_language_data[index];
                break;
            }
        }
        if (config == null) {
            console.log('Lang config is null ' + title_str);
            return title_str;
        }
        if (this.m_language_code.indexOf('zh') >= 0) {
            if (this.m_language_code.indexOf('cn') >= 0) {
                return config.en;
            } else {
                return config.tw;
            }
        } else if (this.m_language_code.indexOf('en') >= 0) {
            return config.en;
        } else if (this.m_language_code.indexOf('ja') >= 0) {
            return config.ja;
        } else if (this.m_language_code.indexOf('ko') >= 0) {
            return config.ko;
        } else if (this.m_language_code.indexOf('fr') >= 0) {
            return config.fr;
        } else if (this.m_language_code.indexOf('de') >= 0) {
            return config.de;
        } else if (this.m_language_code.indexOf('th') >= 0) {
            return config.th;
        } else if (this.m_language_code.indexOf('vi') >= 0) {
            return config.vi;
        } else {
            return config.en;
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
